package com.object.module.didi.service.impl;

import com.object.module.didi.service.DdUserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.object.configuration.utils.PageUtils;
import com.object.configuration.utils.Query;

import com.object.dao.didi.DdOrderFormDao;
import com.object.module.didi.entity.DdOrderFormEntity;
import com.object.module.didi.service.DdOrderFormService;


@Service("ddOrderFormService")
public class DdOrderFormServiceImpl extends ServiceImpl<DdOrderFormDao, DdOrderFormEntity> implements DdOrderFormService {

    @Autowired
    private DdUserService ddUserService;


    /**
     * 分页查询 +可根据当前用户的id来查询
     *
     * @param params
     * @return
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<DdOrderFormEntity> wrapper = new QueryWrapper<>();
        String userId = (String) params.get("urId");
        if (!StringUtils.isEmpty(userId)) {
            wrapper.eq("of_ur_id", userId);
        }
        IPage<DdOrderFormEntity> page = this.page(
                new Query<DdOrderFormEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    /**
     * 添加新的订单
     *
     * @param ddOrderForm
     * @return
     */
    @Override
    public boolean saveForm(DdOrderFormEntity ddOrderForm) {
        ddOrderForm.setOfCreateTime(new Date());
        boolean save = save(ddOrderForm);
        return save;
    }

    /**
     * 根据用户id来查询章节全部的订单
     *
     * @param urId
     * @return
     */
    @Override
    public List<DdOrderFormEntity> Userall(Integer urId) {
        QueryWrapper<DdOrderFormEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("of_ur_id", urId)
                .orderByDesc("of_create_time");
        return list(wrapper);
    }

    @Override
    public List<DdOrderFormEntity> listAll() {
        List<DdOrderFormEntity> list = list();
        for (DdOrderFormEntity ddOrderFormEntity : list) {
             ddOrderFormEntity.setUserEntity(ddUserService.getById(ddOrderFormEntity.getOfUrId()));
        }
        return list;
    }

}